let questionDetailApp = new Vue({
    el: '#questionDetailApp',
    data: {
        question: {}
    },
    methods: {
        loadQuestionDetail: function () {
            // alert("准备加载")
            let id = location.search.substring(1);
            if (id == "" || isNaN(id)) {
                alert("获取id失败，无法加载问题的详情！")
                location.href = '/index.html'
                return
            }
            $.ajax({
                url: '/api/v1/questions/' + id,
                success: function (json) {
                    if (json.state ==2000){
                        let question = json.data
                        question.createdTimeText = getTimeText(question.createdTime)
                        questionDetailApp.question = question
                    }else {
                        alert("获取问题的详情失败,数据不存在，或已被删除！")
                        location.href = '/index.html'
                    }
                }
            })
        }
    },
    created: function () {
        this.loadQuestionDetail()
    }
})

let answersApp = new Vue({
    el:'#answersApp',
    data:{
        answers:[]
    },
    methods: {
        // 提交问题答案
        postAnswer: function () {
            // alert("发表答案")
            let questionId = parseInt(location.search.substring(1))
            let content = $('#summernote').val()
            $.ajax({
                url:'/api/v1/answers/post',
                data:{
                    questionId:questionId,
                    content:content
                },
                dataType:'json',
                type:'POST',
                success:function (json) {
                    if (json.state == 2000){
                        alert("发表答案成功！")
                        // $('#summernote').val(null)
                        $('#summernote').summernote('reset')
                        // 页面滚动都答案列表顶部
                        location.href="#answers"
                        // 重新加载整个答案列表
                        answersApp.loadAnswers()
                    }else {
                        alert(json.message)
                    }
                }
            })
        },
        // 显示问题答案
        loadAnswers:function () {
            // alert("答案列表")
            let questionId = parseInt(location.search.substring(1))
            $.ajax({
                url:'/api/v1/answers',
                data:'questionId='+questionId,
                success:function (json) {
                    let list = json.data
                    let answers = []
                    for (let i=0;i<list.length;i++){
                        let answer = {}
                        answer.id = list[i].id
                        answer.userNickName = list[i].userNickName
                        answer.createdTimeText = getTimeText(list[i].createdTime)
                        answer.content = list[i].content
                        answer.comments = list[i].comments
                        answers[i] = answer
                    }
                    answersApp.answers = answers
                }
            })
        },
        // 显示评论
        postComment:function (answerId,index) {
            // alert("准备提交答案id="+answerId+"的评论!")
            let content = $('#comment-content-'+answerId).val()
            // alert(content)
            $.ajax({
                url:'/api/v1/comments/post',
                data:{
                    answerId:answerId,
                    content:content
                },
                type:'POST',
                success:function (json) {
                    if (json.state==2000){
                        alert("评论成功！")
                        // 清空文本域
                        $('#comment-content-'+answerId).val(null)
                        // 收起评论区
                        $('#answer-comment-'+answerId).collapse('hide')
                        // 更新评论列表,将数据插入到列表的最顶部
                        answersApp.answers[index].comments.unshift(json.data)
                        // 页面滚动到答案顶部
                        location.href = '#answer-'+index
                    }else {
                        alert(json.message)
                    }

                }
            })
        },
        // 删除评论
        deleteComment:function (comments,index) {
            let confirmMessage = "即将执行删除操作，确定要删除吗？"
            if (!confirm(confirmMessage)){
                return
            }
            let commentId = comments[index].id
            $.ajax({
                url:'/api/v1/comments/'+commentId+'/delete',
                type:'POSt',
                success:function (json) {
                    if (json.state==2000){
                        alert("删除成功!")
                        comments.splice(index,1)
                    }else {
                        alert(json.message)
                    }
                }
            })
        }

    },
    created:function () {
        this.loadAnswers()
    }
})

